/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    DongPeng
 * 创建时间:    2023-10-10
 * copyright(c) 2022 无锡星智数服科技有限公司
 */
package org.jeecg.modules.dock.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.dock.client.MesRequestClient;
import org.jeecg.modules.dock.config.JsonUtils;
import org.jeecg.modules.dock.constant.DocumentTask;
import org.jeecg.modules.dock.constant.SystemConstants;
import org.jeecg.modules.dock.entity.DockDataSendRecord;
import org.jeecg.modules.dock.entity.mes.request.MaterialZJPlanProductionLine;
import org.jeecg.modules.dock.entity.mes.request.MesBaseRequestVO;
import org.jeecg.modules.dock.entity.mes.request.MesBaseZLRequestVO;
import org.jeecg.modules.dock.entity.mes.response.MaterialZJPlanProductionLineDetails;
import org.jeecg.modules.dock.entity.mes.response.MesBaseResponseVO;
import org.jeecg.modules.dock.entity.mes.response.PalletCodeQueryResponseVO;
import org.jeecg.modules.dock.enums.DockDataStatus;
import org.jeecg.modules.dock.service.IDockDataSendRecordService;
import org.jeecg.modules.dock.service.MesDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * mes业务对接服务实现
 *
 * @author DongPeng
 * @version 1.0.0
 * @date 2023-10-10
 * @description 类描述
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MesDockingRepeaterServiceImpl implements MesDockingRepeaterService {

    private final MesRequestClient mesRequestClient;

    private final IDockDataSendRecordService dockDataSendRecordService;

    @Override
    public List<MaterialZJPlanProductionLineDetails> getMaterialZJPlanProductionLine(MaterialZJPlanProductionLine request) {
        DockDataSendRecord record = getRecord(JSON.toJSONString(request));
        List<MaterialZJPlanProductionLineDetails> list = new ArrayList<>();
        MesBaseResponseVO dto = mesRequestClient.getMaterialZJPlanProductionLine(request.getTaskType(), request);
        if (ObjectUtil.isEmpty(dto)) {
            log.info("获取Mes原卷纵剪计划产线接口出现异常, 数据id: {}, 时间: {}", record.getUuid(), DateUtil.date());
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultCode(DockDataStatus.FAILURE.getCode());
        } else {
            record.setStatus(DockDataStatus.SUCCESS);
            record.setResultCode(DockDataStatus.SUCCESS.getCode());
            record.setResultData(DockDataStatus.SUCCESS.getRemark());
            log.info("获取Mes原卷纵剪计划产线接口发送成功");
            if (ObjectUtil.isNotEmpty(dto.getData())) {
                list = (List<MaterialZJPlanProductionLineDetails>) dto.getData();
            }
        }
        dockDataSendRecordService.save(record);
        return list;
    }

    @Override
    public MesBaseResponseVO updateStatus(MesBaseRequestVO vo) {
        DockDataSendRecord record = getRecord(JsonUtils.toJson(vo));
        MesBaseResponseVO mesBaseResponseVO = null;
        try {
            mesBaseResponseVO = mesRequestClient.updateStatus(vo.getTaskType(), vo);
            record.setStatus(DockDataStatus.SUCCESS);
            record.setResultCode(CommonConstant.ANNOUNCEMENT_SEND_STATUS_0);
            record.setResultData(mesBaseResponseVO.toString());
        } catch (Exception e) {
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultData(e.toString());
            record.setResultCode(CommonConstant.ANNOUNCEMENT_SEND_STATUS_1);
            log.error("更新MES状态异常信息: ", e);
            return MesBaseResponseVO.failure(e.getMessage());
        } finally {
            dockDataSendRecordService.save(record);
        }
        return mesBaseResponseVO;
    }

    @Override
    public MesBaseResponseVO updateZLStatus(MesBaseZLRequestVO vo) {
        DockDataSendRecord record = getRecord(JsonUtils.toJson(vo));
        MesBaseResponseVO mesBaseResponseVO = null;
        try {
            mesBaseResponseVO = mesRequestClient.updateZLStatus(vo.getTaskType(), vo);
            record.setStatus(DockDataStatus.SUCCESS);
            record.setResultCode(CommonConstant.ANNOUNCEMENT_SEND_STATUS_0);
            record.setResultData(mesBaseResponseVO.toString());
        } catch (Exception e) {
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultData(e.toString());
            record.setResultCode(CommonConstant.ANNOUNCEMENT_SEND_STATUS_1);
            log.error("更新MES状态异常信息: ", e);
            return MesBaseResponseVO.failure(e.getMessage());
        } finally {
            dockDataSendRecordService.save(record);
        }
        return mesBaseResponseVO;
    }

    @Override
    public MesBaseResponseVO palletCodeQuery(String palletCodeQuery) {
        DockDataSendRecord record = getRecord(JsonUtils.toJson(palletCodeQuery));
        MesBaseResponseVO mesBaseResponseVO = null;
        try {
            PalletCodeQueryResponseVO palletCodeQueryResponseVO = mesRequestClient.palletCodeQuery(palletCodeQuery);
            record.setStatus(DockDataStatus.SUCCESS);
            record.setResultCode(CommonConstant.ANNOUNCEMENT_SEND_STATUS_0);
            record.setResultData(palletCodeQueryResponseVO.toString());
        } catch (Exception e) {
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultData(e.toString());
            record.setResultCode(CommonConstant.ANNOUNCEMENT_SEND_STATUS_1);
            log.error("更新MES状态异常信息: ", e);
            return MesBaseResponseVO.failure(e.getMessage());
        } finally {
            dockDataSendRecordService.save(record);
        }
        return mesBaseResponseVO;
    }


    private DockDataSendRecord getRecord(String data) {
        DockDataSendRecord sendRecord = new DockDataSendRecord();
        sendRecord.setSource(SystemConstants.WMS);
        sendRecord.setTarget(SystemConstants.MES);
//        sendRecord.setBusinessCode(SapInterfaceTypeEnum.SALE_OUTBOUND_POST.getCode());
        sendRecord.setUuid(IdUtil.fastSimpleUUID());
        sendRecord.setSendTime(new Date());
        sendRecord.setData(data);
        return sendRecord;
    }
}
